<template>
  <div class="rank">
    <h4 class="title">{{ title }}</h4>
    <div id="review_box_2" @mouseover="rollStop()" @mouseout="rollStart(30)"  style="height: 100%">
      <ul class="list" id="comment1_2">
        <li :key="index" v-for="(item, index) in list">
          <span :class="index < 3 ? 'active' : null">{{ index + 1 }}</span>
          <span>{{ item.name }}</span>
          <span>{{ item.total }}</span>
        </li>
      </ul>
      <ul v-if="showUL2" class="list" id="comment2_2"></ul>
    </div>
  </div>
</template>

<script lang="ts">
import {defineComponent, nextTick, onBeforeUnmount, onMounted, ref} from 'vue';
  export default defineComponent({
    name: 'RankList',
    props: {
      title: {
        type: String,
        default: '',
      },
      list: {
        type: Array,
        default: null,
      },
      height: {
        type: Number,
        default: null,
      },
    },
    setup(props) {
      onMounted(() => {
        let o = document.getElementById('comment1_2');
        let h = o.offsetHeight; //高度
        if (h < props.height) {
          showUL2.value = false;
        }else {
          showUL2.value = true;
        }
        roll(20);
      });
      onBeforeUnmount(() => {
        rollStop();
      });
      const timer = ref(null);
      const showUL2 = ref(true);
      function roll(t) {
        let ul1 = document.getElementById('comment1_2');
        let ul2 = document.getElementById('comment2_2');
        let ulbox = document.getElementById('review_box_2');
        ul2.innerHTML = ul1.innerHTML;
        ulbox.scrollTop = 0;
        rollStart(t);
      }
      function rollStart(t) {
        let ul1 = document.getElementById('comment1_2');
        let ul2 = document.getElementById('comment2_2');
        let ulbox = document.getElementById('review_box_2');
        rollStop();
        timer.value = setInterval(() => {
          // 当滚动高度大于列表内容高度时恢复为0
          if (ulbox.scrollTop >= ul1.scrollHeight) {
            ulbox.scrollTop = 0;
          } else {
            ulbox.scrollTop++;
          }
        }, t);
      }
      function rollStop() {
        clearInterval(timer.value);
      }
      return {
        timer,
        roll,
        rollStart,
        rollStop,
        showUL2,
      };
    },
  });
</script>

<style lang="less" scoped>
  .rank {
    padding: 0 16px 16px 36px;
    width: 100%;
    height: 100%;
    .list {
      margin: 25px 0 0;
      padding: 0;
      list-style: none;

      li {
        margin-top: 16px;

        span {
          color: rgba(0, 0, 0, 0.65);
          font-size: 14px;
          line-height: 22px;

          &:first-child {
            background-color: #f5f5f5;
            border-radius: 20px;
            display: inline-block;
            font-size: 12px;
            font-weight: 600;
            margin-right: 24px;
            height: 20px;
            line-height: 20px;
            width: 20px;
            text-align: center;
          }

          &.active {
            background-color: #314659;
            color: #fff;
          }

          &:last-child {
            float: right;
          }
        }
      }
    }
  }

  .mobile .rank {
    padding: 0 32px 32px 12px;
  }
  #review_box_2 {
    height: 100px;
    overflow: hidden;
  }
</style>
